import urlConfig from './config.js'
import kdup from './kdup.js'

const request = {};
// post请求封装,已通过测试

request.postRequest = (my_url, data) => {
	if(kdup.isEmpty(uni.getStorageSync("kdup_url"))){
		uni.showToast({
			icon: 'none',
			title: '请先配置服务地址！',
			duration: 5000
		});
		return;
	}
	var promise = new Promise((resolve, reject) => {
		data.userId = kdup.userInfo.userid;
		//如果kdup.userInfo.account为空就取username
		if(kdup.isEmpty(kdup.userInfo.account)){
			data.operateror = kdup.userInfo.username;
		}else{
			data.operateror = kdup.userInfo.account;
		}
		uni.setStorageSync("operateror",data.operateror);
		if (kdup.config.hasOwnProperty("fconfignumber"))
			data["configNumber"] = kdup.config.fconfignumber;
		if (kdup.config.hasOwnProperty("fconfigid"))
			data["configId"] = kdup.config.fconfigid;
		if (kdup.config.hasOwnProperty("fcloudbillkey"))
			data["cloudBillKey"] = kdup.config.fcloudbillkey;
		if (kdup.config.hasOwnProperty("finorout"))
			data["inOrOut"] = kdup.config.finorout;
		data["billId"] = kdup.fid;
		if (kdup.config.hasOwnProperty("ishassource"))
			data["isHasSource"] = kdup.config.ishassource;
		var params = JSON.stringify(data);
		var postData = {
			userToken: kdup.userInfo.usertoken,
			param: params
		};
		//console.log(JSON.stringify(postData))
		// uni.showLoading({title: '数据处理中',mask:true});
		uni.request({
			url: uni.getStorageSync("kdup_url")+ "/app/" + my_url,
			data: postData,
			method: "POST",
			async : false,
			header: {
				"content-type": "application/x-www-form-urlencoded"
			},
			sslVerify: false,  
			success: function(res) {
				//关闭加载中
				// setTimeout(function () {uni.hideLoading();}, 100);
				//返回什么就相应的做调整
				if (res.statusCode == 200) {
					if (!kdup.isEmpty(res.data.error)) {
						uni.showToast({
							icon: 'none',
							title: res.data,
							duration: 3000
						});
					}
					resolve(res);
				} else if (res.statusCode == 405) {
					uni.showToast({
						icon: 'none',
						title: '请求方法错误',
						duration: 3000
					});
				} else if (res.statusCode == 401) {
					uni.showToast({
						icon: 'none',
						title: '未登录或登录状态已超时',
						duration: 3000
					});
				} else {
					uni.showToast({
						icon: 'none',
						title: '请求错误:' + res.statusCode,
						duration: 3000
					});
					// 请求服务器成功，但是由于服务器没有数据返回，此时无code。会导致这个空数据
					//接口后面的then执行
					// 不下去，导致报错，所以还是要resolve下，这样起码有个返回值，
					//不会被阻断在那里执行不下去！
					resolve(res.data.msg);
				}
				uni.hideLoading();
			},
			fail: function(err) {
				//关闭加载中
				// setTimeout(function () {uni.hideLoading();}, 600);
				uni.showToast({
					icon: 'none',
					title: "请检查手机是否开启移动数据,或者连接WiFi如果开启了,请认真检查服务器地址!",
					duration: 3000
				});
				reject(err);
				uni.hideLoading();
			}
		});
	});
	return promise;
}

// get请求封装
request.getRequest = (my_url, data) => {
	var promise = new Promise((resolve, reject) => {
		var that = this;
		var postData = data;
		uni.request({
			url: urlConfig + my_url,
			data: postData,
			method: "GET",
			async : false,
			dataType: "json",
			header: {
				"content-type": "application/json"
			},
			sslVerify: false,  
			success: function(res) {
				if (res.statusCode == 200) {
					resolve(res.data);
				} else {
					resolve(res.data);
				}
			},
			fail: function(e) {
				reject("网络出错");
			}
		});
	});
	return promise;
}

export default request
